﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace QuizEngine.Data
{
    public abstract class DbContext : IDisposable
    {
        private PropertyInfo[] _properties;
        protected DbContext()
        {
            Initialize();
        }

        protected void Initialize()
        {
            _properties = this.GetType().GetProperties();
            foreach (PropertyInfo property in _properties)
            {
                if (property.GetValue(this, null) == null)
                {
                    var obj = Activator.CreateInstance(property.PropertyType);
                    property.SetValue(this, obj, null);
                }
            }
        }

        #region Dispose...
        private bool _isDisposed = false;

        ~DbContext()
        {
            _dispose(false);
        }

        protected void _dispose(bool disposing)
        {
            if (disposing)
            {
                // Code to dispose the managed resources of the class
            }
            // Code to dispose the un-managed resources of the class

            _isDisposed = true;
        }

        public void Dispose()
        {
            _dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
